import { mapGetters } from 'vuex'
import { validPhoneCode, validPwd } from '@/utils/validate'
import { reportWithShorid } from '@/api/common'
import i18n from '@/lang'
export default {
  data () {
    return {
      error: 'error',
      iconError: 'icon-error'
    }
  },
  computed: {
    ...mapGetters(['appCfg'])
  },
  methods: {
    async onLoginFinish (ops, callback) {
      const userInfo = {
        status: 1,
        info: {
          isAutoLogin: ops.isAutoLogin,
          code: ops.code,
          uid: ops.uid + '',
          username: ops.user_name,
          headimgurl: ops.head_image,
          expire: ops.expire,
          svr_time: ops.svr_time
        }
      }
      const shortId = this.appInfo.semShortId
      this.$store.dispatch('user/setToken', ops.code)
      localStorage.setItem('loginType', ops.loginType)
      this.$xzAsy('xz_get_stat_id').then(res => {
        if (/^8\d{3}$/.test(res)) {
          this.$xz('xz_substat_action', parseInt(res + '9002'))
        }
      })
      if (ops.is_first_login && shortId) {
        // 注册后上报sem推广
        await reportWithShorid({
          project: `${this.appCfg.name}_Ocpc`,
          shortId,
          newType: 25
        })
      }
      if (this.isPage && process.env.VUE_APP_NAME !== 'i18n') {
        const query = this.$route.query
        const guide = query.guide
        const _from = query.from

        await Promise.allSettled([
          this.$store.dispatch('user/drawTrial'),
          this.$store.dispatch('user/drawPrize')
        ]).then(res => {
          const data = res[0].value
          this.$xz('xz_set_userInfo', JSON.stringify(userInfo))
          if (guide === '0' || data) {
            this.$xz('xz_switch_dialog', 'XZDesktopTrySuccess', true, JSON.stringify({
              from: _from || '',
              guide: guide || '',
              c_pkgids: data
            }))
          }
        })
      } else {
        this.$xz('xz_set_userInfo', JSON.stringify(userInfo))
        this.$store.dispatch('user/getUserInfo')
      }
      this.$store.dispatch('user/setACDialogType', '')
      typeof callback === 'function' && callback()
    },
    needDelDevice (data) {
      const obj = {
        Nickname: data.user_name,
        Portrait: data.head_image,
        user_id: -1000,
        code: data.code,
        is_first_login: data.is_first_login,
        expire: data.expire,
        svr_time: data.svr_time,
        max_allow_device: data.max_allow_device,
        need_del_device: data.need_del_device,
        isAutoLogin: data.isAutoLogin,
        loginType: data.loginType
      }
      this.$store.dispatch('user/setToken', data.code)
      this.$store.dispatch('user/setUserInfo', obj)
      if (this.isPage) {
        this.$xz('xz_switch_dialog', 'XZDesktopDevice', true, JSON.stringify(obj))
      }
      if (process.env.VUE_APP_NAME !== 'i18n') {
        this.$store.dispatch('user/setACDialogType', '')
      }
    },
    emailRules (value) {
      const mailReg = /^[A-Za-z0-9-._\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
      if (!value) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.gmail'), delay: 200 })
        return false
      }
      if (!mailReg.test(value)) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.emailFormat'), delay: 200 })
        return false
      }
      return true
    },
    pwdRules (value1, value2, isCheck) {
      if (!value1) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.pwd'), delay: 200 })
        return false
      }
      if (!validPwd(value1)) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.pwdFormat'), delay: 200 })
        return false
      }
      if (isCheck && value2) {
        return this.pwd2Rules(value1, value2)
      } else {
        return true
      }
    },
    pwd2Rules (value1, value2) {
      if (!value2) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.dif'), delay: 200 })
        return false
      }
      if (value1 !== value2) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.dif1'), delay: 200 })
        return false
      }
      return true
    },
    codeRules (value) {
      if (!value) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.code'), delay: 200 })
        return false
      }
      if (!validPhoneCode(value)) {
        this.$PsMessage({ iconClass: this.error, currentClass: this.iconError, message: i18n.t('code.codeFormat'), delay: 200 })
        return false
      }
      return true
    }
  }
}
